package com.dfbz.demo06_反射小案例;

import com.dfbz.demo02_Class.Cate;
import org.junit.Test;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Properties;

/**
 * @author lscl
 * @version 1.0
 * @intro:
 */
public class Demo01 {


    @Test
    public void test1() throws Exception {

        Class<Demo01> demo01Class = Demo01.class;
        ClassLoader classLoader = demo01Class.getClassLoader();
        InputStream is = classLoader.getResourceAsStream("cate.properties");

        Properties prop = new Properties();
        prop.load(is);

        String className = prop.getProperty("class");
        Class<Cate> cateClass = (Class<Cate>) Class.forName(className);

        Cate cate = cateClass.newInstance();

//        Constructor<Cate> constructor = cateClass.getConstructor(null);
//        Cate cate = cateClass.newInstance();
        String name = prop.getProperty("name");
        String taste = prop.getProperty("taste");
        Boolean recommend = Boolean.parseBoolean(prop.getProperty("recommend"));

//        cate.setName(name);
//        cate.setTaste(taste);
//        cate.setRecommend(recommend);

        Field nameField = cateClass.getDeclaredField("name");
        Field tasteField = cateClass.getDeclaredField("taste");
        Field recommendField = cateClass.getDeclaredField("recommend");

        nameField.setAccessible(true);
        tasteField.setAccessible(true);
        recommendField.setAccessible(true);

        nameField.set(cate,name);
        tasteField.set(cate,taste);
        recommendField.set(cate,recommend);

        System.out.println(cate);
    }
}
